package com.nodecollege.cloud.service.impl.deal;

import com.alibaba.fastjson.JSON;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.po.ChatGroup;
import com.nodecollege.cloud.common.model.po.ChatGroupUser;
import com.nodecollege.cloud.common.model.vo.ChatGroupData;
import com.nodecollege.cloud.common.utils.ThreadUserUtils;
import com.nodecollege.cloud.service.ChatService;
import com.nodecollege.cloud.service.GroupService;
import com.nodecollege.cloud.service.impl.ChatServiceAbstrat;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 修改群名称消息
 *
 * @author LC
 * @date 2020/2/26 23:07
 */
@Slf4j
@Service
public class ChatUpdateGroupServiceImpl extends ChatServiceAbstrat implements ChatService {

    @Autowired
    private GroupService groupService;

    /**
     * 消息处理
     *
     * @param chatData
     * @param channel
     */
    @Override
    public void handler(String chatData, Channel channel) {
        log.info("ChatUpdateGroupServiceImpl handler");
        ChatGroupData groupData = JSON.parseObject(chatData, ChatGroupData.class);
        ChatGroupUser groupUser = new ChatGroupUser();
        groupUser.setUserId(ThreadUserUtils.getLoginUser().getUserId());
        groupUser.setGroupId(groupData.getGroupId());
        groupUser.setGroupName(groupData.getGroupName());
        ChatGroup group = groupService.updateGroup(groupUser);
        // 给群组发送
        sendGroupMsg(group.getGroupId(), NCResult.ok(group), 9);
    }
}
